package ua.kharkov.knure.pereverziev.diploma.web.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * I18n support.
 * 
 * @author A.Pereverziev
 * 
 */
public class ChangeLocaleCommand implements Command {

	private static final long serialVersionUID = -4635843246049903037L;

	private static final Logger log = Logger
			.getLogger(ChangeLocaleCommand.class);

	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		log.debug("Command starts");

		String currentLocale = (String) request.getSession().getAttribute(
				"currentLocale");
		log.trace("Session attribute: currentLocale --> " + currentLocale);

		String locale = request.getParameter("locale");
		log.trace("Parameter from the request: locale --> " + locale);

		request.getSession().setAttribute("currentLocale", locale);
		log.trace("Set the session attribute: currentLocale --> " + locale);

		String lastPage = (String) request.getSession()
				.getAttribute("lastPage");
		log.trace("Session attribute: lastPage --> " + lastPage);

		if (lastPage == null)
			lastPage = "/login.jsp";
		log.debug("Command finished");
		return lastPage;
	}

}